<?php
require_once('src/service/BaseService.php');
require_once('src/vo/EventVO.php');
 
class ExampleService extends BaseService {
  
  /* http://www.doctrine-project.org/documentation/manual/1_1/en/dql-doctrine-query-language */
  
  public function getQueryExample1() {   
     $events = Doctrine::getTable('EVENTS')->findByGameId(6); // WHERE game_id = 6
     $result = $this->transformObjectsToVO($events, "EventVO");    
     return $result; 
  }  
  
  public function getQueryExample2() {   
     $event = Doctrine::getTable('EVENTS')->find(9); // by primary key
     $result = new EventVO();
     $result->updateFromObject($event);  
     return $result; 
  }  
  
  public function getQueryExample3() {   
     $q = Doctrine_Query::create()
    ->select('COUNT(*) as totalGames')
    ->from('GAMES');

     $result = $q->execute(); 
     return $result[0]->totalGames;
    
     // only first object
     // $result = $q->fetchOne(); 
     // return $result->totalGames;

     // this will make $result = direct result, not array     
     // $result = $q->execute(array(), Doctrine::HYDRATE_SINGLE_SCALAR);
     // return $result
  }    
  
  public function getQueryExample4() {   
      $q = Doctrine_Query::create()
      ->select('e.id, f.name')
      ->from('EVENTS e')
      ->leftJoin('FLAGS f ON e.flag_id = f.id') // joins are possible only when relation is specified
      ->limit(20);     
    
      // this is workaround, but the result is array, not Doctrine object.
      // so it is not translated to VO here, the translation has to be done manually, crappy
     $result = $q->execute(array(), Doctrine::HYDRATE_SCALAR);  
     return $result; 
  }      
  
  public function getQueryExample5byPDO() {  
      $pdo = $this->conn->getDbh();
      $q = 'SELECT e.id as eventId, f.name as flagName FROM events e LEFT JOIN flags f ON e.flag_id = f.id LIMIT 20';
      $result = $pdo->query($q)->fetchAll();
//      $result = $this->transformArrayToVO($result, "PlainQueryResultVO");    
      return $result; 
  }      
  
}
?>